﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;

namespace STF.OrangeDog.Service
{
    public class ReplyBll:IReplyBll
    {
        private IReplyDal _db;

        public ReplyBll(IReplyDal db)
        {
            _db = db;
        }
        public async Task<int> AddReplyAsync(int Pid, string Content, int ParentId, int GoodPraise, int Uid)
        {
            return await _db.AddModelAsync(new Reply
            {
                pid = Pid,
                content = Content,
                parentId = ParentId,
                GoodPraise = GoodPraise,
                uid = Uid
            });
        }

        public async Task<int> DeleteReplyAsync(int Id)
        {
            var data = await _db.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            if (data.DeleteId == 1)
                return 0;
            return await _db.RemoveModelAsync(data);
        }

        public async Task<int> EditReplyAsync(int Id, int Pid, string Content, int ParentId, int GoodPraise, int Uid)
        {
            var data = await _db.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            data.pid = Pid;
            data.content = Content;
            data.parentId = ParentId;
            data.GoodPraise = GoodPraise;
            data.uid = Uid;
            data.UpdateTime = DateTime.Now;
            return await _db.EditModelAsync(data);
        }

        public async Task<IEnumerable<ReplyDtos>> GetAll(int DeleteId = 1)
        {
            var data = await _db.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
            return data != null ? data.Select(r => new ReplyDtos
            {
                id = r.Id,
                content = r.content,
                parentId = r.parentId,
                GoodPraise = r.GoodPraise,
                uid = r.uid,
               
            }) : new List<ReplyDtos>();
        }

        public async Task<ReplyDtos> GetDataById(int Id)
        {
            var data = await _db.GetDataByIdAsync(Id);
            return data == null ? new ReplyDtos() : new ReplyDtos
            {
                id = data.Id,
                content = data.content,
                parentId = data.parentId,
                GoodPraise = data.GoodPraise,
                uid = data.uid,
                
            };
        }
    }
}
